package com.web.biz.dlzc.service;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordSecurityUtils;
import com.css.sword.kernel.utils.SwordSequenceUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_APP_ANDROID;
import com.web.common.dataDictionary.DE.DE_APP_IOS;
import com.web.common.dataDictionary.DE.DE_C_BRAND_ID;
import com.web.common.dataDictionary.DE.DE_C_BRAND_OPPTIME;
import com.web.common.dataDictionary.DE.DE_C_BRAND_XTBPATH;
import com.web.common.dataDictionary.DE.DE_ID;
import com.web.common.dataDictionary.DE.DE_OPPTIME;
import com.web.common.dataDictionary.DE.DE_PASSWORD;
import com.web.common.dataDictionary.DE.DE_SHFJ;
import com.web.common.dataDictionary.DE.DE_SHSJ;
import com.web.common.dataDictionary.DE.DE_SHZT;
import com.web.common.dataDictionary.DE.DE_VIPSYTS;
import com.web.common.dataDictionary.DE.DE_WEBSHSJ;
import com.web.common.dataDictionary.DE.DE_WEBSHZT;
import com.web.common.dataDictionary.DE.DE_WEBUSERNAME;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.dataDictionary.PO.TabUserPO;
import com.web.common.utils.CollectionUtil;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.ParamConfig;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class LoginRegisterService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(LoginRegisterService.class);
	public LoginRegisterService() throws Exception {
		super();
	}

	/**
	 * 登录到系统主界面
	 * @param req
	 * @return
	 */
	@Service(serviceName="LoginRegisterService_main")
	public IResData main(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterService_main 方法。");
		String yhm = LoginInfoUtil.getLoginUserName();
		IResData res = new SwordRes();
		if (!"".equals(yhm)) {
			res.addJSTL("yhm", yhm);
			res.addPage("/pages/login/frames.jsp");
		}else {
			//session超时后跳转的页面
			res.addPage("/pages/login/timeout.jsp");
		}		
		return res;
	}
	
	 		
	/**
	 * 用户登录验证
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Service(serviceName = "LoginRegisterService_login")
	public IResData login(IReqData req) throws Exception {

		log.debug("开始进入 LoginRegisterService_login 方法。");
		String yhm = req.getForm("form_denglu").get("yhdm");
		String password = req.getForm("form_denglu").get("mm");	

		String pwdMD5 = SwordSecurityUtils.md5(password.getBytes());
		Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		String username = "";
		IResData res = new SwordRes();
		if (CollectionUtil.isNotBlankMap(resMap)) {
			String realPwd = StringUtil.parse2String(resMap.get("password"));
			if(pwdMD5.equals(realPwd)){
				username = StringUtil.parse2String(resMap.get("username"));
				res.addAttr("username", username);
				res.put("yhm", username);
			}else{
				res.addAttr("msg", "密码错误！请重新输入！");
			}
		}else{
			res.addAttr("msg", "用户名错误！请重新输入！");
			res.addAttr("yhm", "");
		}
		return res;
	}
	
	/**
	 * 注册验证登录
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "LoginRegisterService_registerlogin")
	public IResData registerlogin(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterService_login 方法。");
		String yhm = (String) req.getAttr("yhm");
		String password = (String) req.getAttr("mm");	
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String pwdMD5 = SwordSecurityUtils.md5(password.getBytes());
		Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		String username = "";
		IResData res = new SwordRes();
		if (resMap!=null&&resMap.size()!=0) {
			String realPwd = (String) resMap.get("userpassword");
			if(pwdMD5.equals(realPwd)){
				username = (String) resMap.get("xm");
				res.addAttr("username", username);
				Map jgsdmap = dao.queryMapByKey("AdminService_queryDefaultJGSD", null);
				String jgsd = "";
				if(jgsdmap!=null){
					jgsd = (String) jgsdmap.get("jgsd");
				}
				res.put("jgsd", jgsd);
				res.put("yhm", yhm);
			}
		}else{
			res.addAttr("msg", "用户名或密码错误！");
			res.addAttr("yhm", "");
		}
		return res;
	}

 		
	/**
	 * 保存注册信息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Service(serviceName = "LoginRegisterService_saveRegister")
	public IResData saveRegister(IReqData req) throws Exception {

		log.debug("开始进入 LoginRegisterService_saveRegister 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		String msg = "";
		try {
			TabUserPO po = (TabUserPO) req.getForm("form_register", TabUserPO.class);
			if (po!=null) {
				String yhm = po.getUsername().value;
				Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
				if (CollectionUtil.isBlankMap(resMap)) {
					//新增用户信息
					String id = SwordSequenceUtils.generateRandomString();
					String pwd = po.getPassword().value;
					String pwdMD5 = SwordSecurityUtils.md5(pwd.getBytes());
					po.setId(new DE_ID(id));
					po.setPassword(new DE_PASSWORD(pwdMD5));
					po.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
					po.setShfj(new DE_SHFJ(StringUtil.parse2String(req.getBizParam("pzfj"))));
					po.setShzt(new DE_SHZT("dsh"));
					po.setShsj(new DE_SHSJ(DateFormatUtil.getNowDate()));
					po.setVipsyts(new DE_VIPSYTS(Integer.valueOf(ParamConfig.getInstanse().getParam("initWealth"))));
					int i = dao.insertPO(po);
					//新增店铺信息
					String shopid = SwordSequenceUtils.generateRandomString();
					TCategoryShopPO shoppo = (TCategoryShopPO) req.getForm("form_register", TCategoryShopPO.class);
					shoppo.setCBrandId(new DE_C_BRAND_ID(shopid));
					shoppo.setWebusername(new DE_WEBUSERNAME(yhm));
					shoppo.setCBrandXtbpath(new DE_C_BRAND_XTBPATH(StringUtil.parse2String(req.getBizParam("tbfj"))));
					shoppo.setWebshzt(new DE_WEBSHZT("wtjsh"));
					shoppo.setWebshsj(new DE_WEBSHSJ(DateFormatUtil.getNowDate()));
					shoppo.setAppIos(new DE_APP_IOS(""));
					shoppo.setAppAndroid(new DE_APP_ANDROID(""));
					shoppo.setCBrandOpptime(new DE_C_BRAND_OPPTIME(DateFormatUtil.getNowDate()));
					int j = dao.insertPO(shoppo);
					
					if (i>0 && j>0) {
						dao.commitAll();
						msg="true";
					}
				}
			}
		} catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
			msg="";
		}
		
		IResData res = new SwordRes();
		res.addAttr("msg", msg);
		return res;
	}
	
	
	/**
	 * 验证用户名是否存在
	 * @param yhm
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Service(serviceName = "LoginRegisterService_validateByYhm")
	public IResData validateByYhm(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterService_validateByYhm 方法。");
		String yhm = (String) req.getAttr("yhm");
		SQLParam param = new SQLParam();
		param.addParam(yhm);
		Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		String result = "";
		if (CollectionUtil.isNotBlankMap(resMap)) {
			result = "exist";
		}
		IResData res = new SwordRes();
		res.addAttr("exist", result);
		return res;
	}
	
	
	/**
	 * 根据用户名查询信息
	 * @param yhm
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "LoginRegisterService_queryByYhm")
	public Map queryByYhm(String yhm) throws Exception {
		log.debug("开始进入 LoginRegisterService_queryByYhm 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		SQLParam param = new SQLParam();
		param.addParam(yhm);
		Map<String, Object> resMap = dao.queryMapByKey("LoginRegisterService_queryByYhm", param);
		return resMap;
	}
	
	
	/**
	 * 根据用户名查询信息
	 * @param yhm
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Service(serviceName = "LoginRegisterService_queryShopxxByYhm")
	public Map queryShopxxByYhm(String yhm) throws Exception {
		log.debug("开始进入 LoginRegisterService_queryShopxxByYhm 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		SQLParam param = new SQLParam();
		param.addParam(yhm);
		Map<String, Object> resMap = dao.queryMapByKey("grxx_queryShopxxByYhm", param);
		return resMap;
	}
	
	/**
	 * 根据用户名查询信息
	 * @param yhm
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "LoginRegisterService_queryTcategoryxx")
	public IResData queryTcategoryxx(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterService_queryTcategoryxx 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		IResData res = new SwordRes();
		//List categoryList = CacheUtil.getPullData("DM_YW_CATEGORY");
		List list = dao.queryMapListByKey("queryTcategory", null);
		res.addListWithDataName("categoryTitle", list);
		return res;
	}

}